package cn.edu.scau.cmi.yangqing.hibernate.client.control;



import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.yangqing.hibernate.domain.Student;
import cn.edu.scau.cmi.yangqing.hibernate.domain.StudentProperty;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Tutor;
import cn.edu.scau.cmi.yangqing.hibernate.domain.TutorProperty;
import cn.edu.scau.cmi.yangqing.hibernate.service.StudentService;
import cn.edu.scau.cmi.yangqing.hibernate.service.TutorService;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import sun.net.www.content.text.plain;

public class adminControl implements Initializable {

	   @FXML
	   private TableView<StudentProperty> studentTable;
	   
	   @FXML
	   private TableColumn<StudentProperty, Integer> ids;
	   
	   @FXML
	   private TableColumn<StudentProperty, String> names;
	   
	   @FXML
	   private TableColumn<StudentProperty, Integer> tutorids;
	   
	   @FXML
	   private TableView<TutorProperty> tutorTable;
	   
	   @FXML
	   private TableColumn<TutorProperty, Integer> idt;
	   
	   @FXML
	   private TableColumn<TutorProperty, String> namet;
	   
	   //private final ObservableList<Student> students = FXCollections.observableArrayList();
	   private final ObservableList<StudentProperty> cellData = FXCollections.observableArrayList();
	   private final ObservableList<TutorProperty> cellDataTutor = FXCollections.observableArrayList();
	   
	   @FXML 
	   private Button createStudentButton;
	   @FXML
	   private TextField createStudentName;
	   
	   @FXML 
	   private Button deleteStudentButton;
	   @FXML
	   private TextField deleteStudentid;
	   
	   @FXML 
	   private Button updateStudentButton;
	   @FXML
	   private TextField updateStudentId;
	   @FXML
	   private TextField updateStudentName;
	   
	   @FXML 
	   private Button createTutorButton;
	   @FXML
	   private TextField createTutorName;
	   
	   @FXML 
	   private Button deleteTutorButton;
	   @FXML
	   private TextField deleteTutorId;
	   
	   @FXML 
	   private Button updateTutorButton;
	   @FXML
	   private TextField updateTutorId;
	   @FXML
	   private TextField updateTutorName;
	   
	   StudentService studentService = new StudentService();
	   TutorService tutorService = new TutorService();
	   
	   public void initialize(URL location, ResourceBundle resources) {
		   //StudentService studentService = new StudentService();
		   List<Student> studentList = studentService.findAllStudent();
		   for (Student student : studentList) {
			   IntegerProperty idProperty = new SimpleIntegerProperty(student.getId());
			   StringProperty nameProperty = new SimpleStringProperty(student.getName());
			   IntegerProperty tutorProperty = new SimpleIntegerProperty(student.getTutor().getId());
			   StudentProperty sProperty = new StudentProperty(idProperty, nameProperty, tutorProperty);
			   cellData.add(sProperty);
		   } 
		   //（2）配置 TableColumn 列
		   ids.setCellValueFactory(cellData -> cellData.getValue().getId().asObject());
		   names.setCellValueFactory(cellData -> cellData.getValue().getName());
		   tutorids.setCellValueFactory(cellData -> cellData.getValue().getIdt().asObject());
		   //（3）为 TableView 添加数据
		   studentTable.setItems(cellData);
		   
		   
		   List<Tutor> tutorList = tutorService.findAllTutor();
		   for (Tutor tutor : tutorList) {
			TutorProperty tutorProperty = new TutorProperty(new SimpleIntegerProperty(tutor.getId()),
					new SimpleStringProperty(tutor.getName()));
				cellDataTutor.add(tutorProperty);
		   }
		   idt.setCellValueFactory(cellDataTutor -> cellDataTutor.getValue().getId().asObject());
		   namet.setCellValueFactory(cellDataTutor -> cellDataTutor.getValue().getName());
		   tutorTable.setItems(cellDataTutor);
	   }

	   public void createStudent(ActionEvent event) {
		   Student student=new Student();
		   student.setName(createStudentName.getText());
		   Tutor tutor = new Tutor();
		   tutor.setId(1);
		   student.setTutor(tutor);
		   studentService.addStudent(student);
	   }
	   public void deleteStudent(ActionEvent event) {
		   studentService.deleteStudent(Integer.valueOf(deleteStudentid.getText()));
	   }
	   public void updateStudent(ActionEvent event) {
		   Integer id = Integer.valueOf(updateStudentId.getText());
		   String name =updateStudentName.getText();
		   Student student = new Student();
		   student = studentService.findStudentById(id);
		   student.setName(name);
		   studentService.updateStudent(student );
	   }
   
	   public void createTutor(ActionEvent event) {
		     Tutor tutor = new Tutor();
		     tutor.setName(createTutorName.getText());
		     tutorService.addTutor(tutor);
	   }
	   public void deleteTutor(ActionEvent event) {
		     tutorService.deleteTutor(Integer.valueOf(deleteTutorId.getText()));
	   }
	   public void updateTutor(ActionEvent event) {
		     Integer id = Integer.valueOf(updateTutorId.getText());
		     String name = updateTutorName.getText();
		     Tutor tutor = new Tutor();
		     tutor = tutorService.findTutorById(id);
		     tutor.setName(name);
		     tutorService.updateTutor(tutor);
	   }
}
